﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YGWebMobileApi.Data;
using OneMile.DAL;
using OneMile.Common;
using OneMile.Entity.Model;
using System.Text;
using OneMile.BLL;

namespace YGWebMobileApi.APIs
{
     [MobileAPI(Code = "MC0205", Name = "用户获取商品提交信息")]
    public class UGetCommodityForCommitById: BaseAPI, IProcessMessage
    {
         private BLLUCommodity bllCommodity;
         protected BLLUCommodity BllCommodity
        {
            get
            {
                if (bllCommodity == null)
                {
                    bllCommodity = BLLFactory.getInstance().CreateUCommodityBLL();
                }
                return bllCommodity;
            }
        }
        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);

                string CommodityId = reqMsg.Body.GetFieldValue("CommodityId");

                if (String.IsNullOrEmpty(CommodityId))
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }

                ECommodity ecommodity = (ECommodity)BllCommodity.GetCommodityById(CommodityId);

                rspMsg.Body.AddField("ShopsId", ecommodity.ShopsId.TString());
                rspMsg.Body.AddField("CommodityId", ecommodity.CommodityId.TString());
                rspMsg.Body.AddField("CommodityName", ecommodity.CommodityName);
                rspMsg.Body.AddField("IsHot", ecommodity.IsHot ? "1" : "0");
                rspMsg.Body.AddField("IsNew", ecommodity.IsNew ? "1" : "0");
                rspMsg.Body.AddField("CommodityRemarks", ecommodity.CommodityRemarks);

                var fl1 = rspMsg.Body.AddFieldList("CommodityAccessoriesList");
                foreach(ECommodityAccessories eca in ecommodity.CommodityAccessoriesList)
                {
                    var fll1 = fl1.AddFieldList("CommodityAccessories");
                    fll1.AddField("CommodityAccessoriesName", eca.CommodityAccessoriesName);
                    fll1.AddField("CommodityAccessoriesPrice", eca.CommodityAccessoriesPrice.TString("F1"));
                    fll1.AddField("CommodityAccessoriesId", eca.CommodityAccessoriesId.TString());
                }

                var fl2 = rspMsg.Body.AddFieldList("SpecificationList");
                foreach (ESpecification es in ecommodity.SpecificationList)
                {
                    var fll2 = fl2.AddFieldList("Specification");
                    fll2.AddField("CommodityPriceId",es.CommodityPriceId.TString());
                    fll2.AddField("SpecificationId",es.SpecificatioId.TString());
                    fll2.AddField("Price",es.Price.TString("F1"));
                    fll2.AddField("SpecificationName",es.SpecificationName);
                    fll2.AddField("Description",es.Description.TString());
                }

                var fl3 = rspMsg.Body.AddFieldList("AttributeList");
                foreach (EAttribute ea in ecommodity.AttributeList)
                {
                    var fll3 = fl3.AddFieldList("Attribute");
                    fll3.AddField("AttributeId",ea.AttributeId.TString());
                    fll3.AddField("AttributeName",ea.AttributeName);
                    fll3.AddField("Description",ea.Description);

                    var flll3 = fll3.AddFieldList("AttributeDetailsList");
                    foreach (EAttributeDetails ead in ea.AttributeDetailsList)
                    {
                        var fllll3 = flll3.AddFieldList("AttributeDetails");
                        fllll3.AddField("AttributeDetailsId",ead.AttributeDetailsId.TString());
                        fllll3.AddField("AttributeDetailsName",ead.AttributeDetailsName);
                    }
                    

                }

                rspMsg.Head.RspCode = MessageCode.Success.Code;
                rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                return rspMsg;
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("UGetCommodityDetailsById-" + e);
                throw e;
            }
        }

        #endregion
    }
}